//
// Copyright (C) 2011 Solar Technology Ltd,
// All rights reserved.
//
// Author: Finux Chen <finuxchen@gmail.com>
//
// This file is part of customized widgets for touch screen.
//
#ifndef _TOUCHSPINBOX_H
#define _TOUCHSPINBOX_H

#include <QWidget>

class QHBoxLayout;
class TouchDecimalSpinBox;
class QPushButton;
class QLabel;
//class QLineEdit;
class QHBoxLayout;
class QVBoxLayout;
class QValidator;
class QTimer;
class TouchLineEdit;

class TouchSpinBoxBase : public QWidget {
	Q_OBJECT

public:
	TouchSpinBoxBase(QWidget *parent = 0);
	~TouchSpinBoxBase();

	inline int value() const
	{
		return value_;
	}
	void setValue(int value);

	void setValueString(const QString &text);
	void setSignString(const QString &text);
	
	QString signText();
	QString lineEditText();
signals:
	void valueChanged();

protected slots:
	virtual void plus();
	virtual void minus();
	void spinPressed();
	void spinReleased();
	void timerTimeOut();
	void captureInput(const QString &text);
	virtual void checkValueText();

protected:
	void InitWidget();

	virtual int calcMaxLength();

	virtual QString valueTextFromAbs();
	void updateText();

	// This is an auxiliary function to avoid overflow situation.
	unsigned int absValueFromText();
	int valueFromText();
	void updateValue();

	enum TimerTarget {
		kTimerPlus = 0,
		kTimerMinus
	};

	QPushButton *plusButton;
	QPushButton *minusButton;
	QLabel      *signLabel;
//	QLineEdit   *valueEdit;
	TouchLineEdit *valueEdit;
	QHBoxLayout *hboxLayout;
	QVBoxLayout *vboxLayout;
	QValidator  *inputValidator;
	QTimer      *spinTimer;
	TimerTarget timerTarget;
	int timer_interval_;

	int value_;
	int max_;
	int min_;
	int step_;
};

class TouchSpinBox : public TouchSpinBoxBase {
	//Q_OBJECT

public:
	TouchSpinBox(QWidget *parent = 0);

	inline int step() const
	{
		return step_;
	}
	inline void setStep(int step)
	{
		step_ = step;
	}

	inline int maximum() const
	{
		return max_;
	}
	inline void setMaximum(int max)
	{
		max_ = max;
	}

	inline int minimum() const
	{
		return min_;
	}
	inline void setMinimum(int min)
	{
		min_ = min;
	}

	inline void setRange(int min, int max)
	{
		setMinimum(min);
		setMaximum(max);
	}
//protected slots:
	//void captureMinusInput(const QString &text);

private:

};

class TouchDecimalSpinBox : public TouchSpinBoxBase {
	Q_OBJECT

public:
	TouchDecimalSpinBox(QWidget *parent = 0);
	//~TouchDecimalSpinBox();

	void setDoubleStep(double step);

protected slots:
	void checkValueText();

private:
	QString valueTextFromAbs();
	int calcMaxLength();

	double d_step_;
	int    max_length_;
};

class TouchDoubleSpinBox : public QWidget {
	Q_OBJECT

public:
	TouchDoubleSpinBox(QWidget *parent = 0);
	~TouchDoubleSpinBox();

	inline double value()
	{
		return value_;
	}
	void setValue(double value);

	inline double maximum()
	{
		return max_;
	}
	void setMaximum(double max);

	inline double minimum()
	{
		return min_;
	}
	void setMinimum(double min);

	void setRange(double min, double max);

	inline double step()
	{
		return step_;
	}
	void setStep(double step);

private slots:
	void checkValue();

private:
	TouchDecimalSpinBox *decimalSpinBox;
	TouchSpinBox *integerSpinBox;
	QHBoxLayout	 *hboxLayout;

	double value_;
	double step_;
	double max_;
	double min_;
};

#endif // _TOUCHSPINBOX_H
